package com.lanou.musicbox.adapter.localmusic;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.musicbox.R;
import com.lanou.musicbox.custom.Events;
import com.lanou.musicbox.entity.Entity;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 15/11/5.
 */
public class PopupWindowAdapter extends BaseAdapter {
    private List<Entity> mList;
    private Context mContext;
    private String what_music;


    public  PopupWindowAdapter( Context context,List<Entity>mList) {
        //注册 EventBus
        EventBus.getDefault().register(this);
        this.mList = mList;

        this.mContext = context;

    }
    public void onEvent(Events.NewSongsEvent musicInfoEvent) {

        what_music = musicInfoEvent.getWhatMusic();
        if (mList != null && mList.size() > 0) {
            mList = musicInfoEvent.getSongsInfos();
            Log.i("lanou555555", "PopupWindowAdapter "+mList.size()+what_music);
        }
        notifyDataSetChanged();

    }


    @Override
    public int getCount() {
        return mList != null && mList.size() > 0 ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        ViweHolderP holderP = new ViweHolderP();
        if (v==null){
            v = LayoutInflater.from(mContext).inflate(R.layout.popup_music_list,null);
            holderP.music_artist = (TextView) v.findViewById(R.id.music_author_name);
            holderP.music_title = (TextView) v.findViewById(R.id.music_album_title);
            holderP.music_count_time = (TextView) v.findViewById(R.id.music_count_time);
            holderP.song_list_num = (TextView) v.findViewById(R.id.music_list_id);
            v.setTag(holderP);
        }
        else {
            holderP = (ViweHolderP) v.getTag();
        }
        if (mList != null&& mList.size()>0){
            holderP.music_title.setText(mList.get(position).getTitle());
            holderP.song_list_num.setText(position + 1 + "");
            Log.i("lanou996584", "getView "+what_music);
//            if (what_music!=null&&!what_music.equals("本地")){
//                holderP.music_count_time.setVisibility(View.GONE);
//            }
//            else {
//                holderP.music_count_time.setText(String.valueOf(formatTime(mList.get(position).getDuration())));
//            }
            holderP.music_artist.setText(mList.get(position).getArtist());
        }

        return v;
    }

    public static String formatTime(Long time) {                     //将歌曲的时间转换为分秒的制度
        String min = time / (1000 * 60) + "";
        String sec = time % (1000 * 60) + "";

        if (min.length() < 2)
            min = "0" + min;
        switch (sec.length()) {
            case 4:
                sec = "0" + sec;
                break;
            case 3:
                sec = "00" + sec;
                break;
            case 2:
                sec = "000" + sec;
                break;
            case 1:
                sec = "0000" + sec;
                break;
        }
        return min + ":" + sec.trim().substring(0, 2);
    }

    public class ViweHolderP {

        private TextView music_title;
        private TextView music_artist;
        private TextView music_count_time,song_list_num;

    }
}
